package gui.actions;

import javax.swing.JOptionPane;

import common.Fictionator;
import common.interfaces.DBInterface;

public class CleanupAction extends WorkerAction {

	private static final long serialVersionUID = 1L;
	private boolean isClean = true;
	private DBInterface dbAccess;
	
	public CleanupAction() {
		dbAccess = Fictionator.getInstance().getDBAccess();
	}
	
	@Override
	public boolean isAllowToRunOperation() {
		return true;
	} 
	
	@Override
	public void runOperation() throws Exception {
		isClean = dbAccess.isCleanDB();
	}
	
	@Override
	public boolean isAllowToRunCommitOperation() {
		if (!isClean) {
	        String message = "There seems to be some redundant data in the database the may affect performance.\n";
	        message += "It is possible to run a cleaner that will clean the database.\nDo you want to run it now?";
	        String title = "Possible cleanup";
	        String[] options = { "Run cleanup", "Maybe next time" };
	  
	        int choice = JOptionPane.showOptionDialog(null, message, title, 0, JOptionPane.INFORMATION_MESSAGE, null, options, null);
	        return choice == 0;	
		}
		return false;
	}
	
	@Override
	public void runCommitOperation() throws Exception {
		dbAccess.garbageCollection();
	}
}
